/*
 * CS_ULTIMATE_EVOLUTION_BATTLE_PET_V2.cpp
 *
 *  Created on: 2014. 2. 19.
 *      Author: mega
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Item.h"
#include "../../NResource/Static.h"

namespace ProjectZ { namespace NCommand {

static void SendFailBattlepetEvolutionV2(User * pUser, int res)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(res);
	pAck->MakePacket(CMD_SC_ULTIMATE_EVOLUTION_BATTLE_PET_V2);
	pUser->Send(pAck);
}

void Command::CS_ULTIMATE_EVOLUTION_BATTLE_PET_V2(User * pUser, ::Network::Packet * pPacket)
{
/*	//전투펫 궁극진화
	CS_ULTIMATE_EVOLUTION_BATTLE_PET_V2		0x3004
	{
		u64 u64base_pet;			//진화시킬 전투펫
		u64 u64material_1;			//재료 1 아이템
		u8	u8material_1_count;		//재료 1 갯수
		u64 u64material_2;			//재료 2 아이템
		u8	u8material_2_count;		//재료 2 갯수
	}
	SC_ULTIMATE_EVOLUTION_BATTLE_PET_V2		0x3005
	{
		s16	u16ack;				//1: 진화 성공 (진화는 실패 없음)
	}*/

	PREPARE_PACKET;

	uint64_t basePetSeq = 0;
	uint64_t materialSeq1 = 0;
	uint64_t materialSeq2 = 0;
	char materialCount1 = 0;
	char materialCount2 = 0;

	*pPacket >> basePetSeq >> materialSeq1 >> materialCount1 >> materialSeq2 >> materialCount2;

	NLogic::Bag * pBag = pUser->GetBag(BAG_TYPE_BATTLE_PET);
	NLogic::Bag * pBagMisc = pUser->GetBag(BAG_TYPE_MISC);

	if (pBag == NULL || pBagMisc == NULL)
	{
		SendFailBattlepetEvolutionV2(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NLogic::Item * pPet		= pBag->GetItem(basePetSeq);
	NLogic::Item * pItem1	= pBagMisc->GetItem(materialSeq1);
	NLogic::Item * pItem2	= pBagMisc->GetItem(materialSeq2);

	if (pPet == NULL || pItem1 == NULL || pItem2 == NULL)
	{
		SendFailBattlepetEvolutionV2(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

//	NLogic::Item::Data & clsData			= pPet->GetData();
//	NLogic::Item::Data & clsDataMaterial1	= pItem1->GetData();
//	NLogic::Item::Data & clsDataMaterial2	= pItem2->GetData();

	NResource::UltimateEvolutionResource::Key	clsKey;
	clsKey.evType	= static_cast<int>(NResource::UltimateEvolutionResource::EvolutionType::EM_ULTIMATE_BATTLEPET);
	clsKey.elType	= pPet->GetData().eff_pos[0];
	clsKey.level	= (pPet->GetData().level >= 10) ? (pPet->GetData().level / 10) * 10 : 1;

	NResource::UltimateEvolutionResource::Flyweight * pFly = NResource::Static::InstancePtr()->GetUltimateResource()->GetFly(clsKey);
	if (NULL == pFly)
	{
		DEBUGLOG("%s : pFly is NULL..", __FUNCTION__);
		SendFailBattlepetEvolutionV2(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	// 전투펫 궁극진화에 필요한 재료 아이템인지 확인
	if (pItem1->GetData().sub_type != static_cast<int>(CLASS_ITEM_TABLE_ETC))
	{
		DEBUGLOG("%s : INVALID ITEM 1 SHEET = %d", __FUNCTION__, pItem1->GetData().sub_type);
		SendFailBattlepetEvolutionV2(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	if (pItem2->GetData().sub_type != static_cast<int>(CLASS_ITEM_TABLE_ETC))
	{
		DEBUGLOG("%s : INVALID ITEM 2 SHEET = %d", __FUNCTION__, pItem1->GetData().sub_type);
		SendFailBattlepetEvolutionV2(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	// 재료의 TID 확인
	if (pFly->_ITEM1_TID != pItem1->GetData().tid)
	{
		DEBUGLOG("%s : INVALID ITEM1 TID=%d", __FUNCTION__, pItem1->GetData().tid);
		SendFailBattlepetEvolutionV2(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	if (pFly->_ITEM2_TID != pItem2->GetData().tid)
	{
		DEBUGLOG("%s : INVALID ITEM2 TID=%d", __FUNCTION__, pItem2->GetData().tid);
		SendFailBattlepetEvolutionV2(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	// 재료의 갯수 확인
	if (pFly->_ITEM1_QUANTITY != materialCount1)
	{
		DEBUGLOG("%s : INVALID ITEM1 QUANTITY=%d", __FUNCTION__, materialCount1);
		SendFailBattlepetEvolutionV2(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	if (pFly->_ITEM2_QUANTITY != materialCount2)
	{
		DEBUGLOG("%s : INVALID ITEM2 QUANTITY=%d", __FUNCTION__, materialCount2);
		SendFailBattlepetEvolutionV2(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	// 재료의 부족 여부 확인
	if (0 > (pItem1->GetData().quantity - materialCount1) )
	{
		SendFailBattlepetEvolutionV2(pUser, ACK_PET_MATERIAL_LACK);
		return;
	}

	if (0 > (pItem2->GetData().quantity - materialCount2) )
	{
		SendFailBattlepetEvolutionV2(pUser, ACK_PET_MATERIAL_LACK);
		return;
	}


	// 궁극 진화
	int ack = NResource::Static::InstancePtr()->GetBattlepetGachya()->BattlePetEvolutionV2(pUser, pPet);

	// 재료 차감
	if (ACK_OK == ack || ACK_BATTLEPET_EVOLUTION_FAIL == ack)
	{
		pItem1->GetData().quantity -= materialCount1;
		pItem2->GetData().quantity -= materialCount2;

		if (pItem1->GetData().quantity <= 0)
		{
			pBagMisc->PopItem(materialSeq1);
			pItem1->DeleteDatabaseItemInfo(NLogic::Item::ItemStatus::EM_ITEM_STATUS_BATTLEPET_EVOLUTION);
		}
		else
		{
			pItem1->UpdateDatabaseItemInfo();
		}

		if (pItem2->GetData().quantity <= 0)
		{
			pBagMisc->PopItem(materialSeq2);
			pItem2->DeleteDatabaseItemInfo(NLogic::Item::ItemStatus::EM_ITEM_STATUS_BATTLEPET_EVOLUTION);
		}
		else
		{
			pItem2->UpdateDatabaseItemInfo();
		}

		// OK인 경우에만 전투펫 갱신
		if (ACK_OK == ack)
		{
			pPet->UpdateDatabaseItemInfo();
		}
	}

	// 응답
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_ULTIMATE_EVOLUTION_BATTLE_PET_V2);
	pUser->Send(pAck);
}

} /* NCommand */ } /* ProjectZ */



